<?php

namespace Addons\Draw\Controller;

use Home\Controller\AddonsController;

class WapController extends AddonsController
{
    function _initialize()
    {
        parent::_initialize();

        $shareData ['title'] = "江苏卫视体育频道赛事直播";
        $shareData ['desc'] = "参与竞赛，助威赢大奖！！";
        $shareData ['imgUrl'] = SITE_URL . '/Public/static/football/share_pic.jpg';
        $shareData ['link'] = addons_url('Sucai://sucai/package', array(
            'is_redirect' => 1,
            'id' => 6,
            'source' => 'Sports'
        ));

        $this->assign('shareData', $shareData);
    }

    function show()
    {
        $sports_id = I('sports_id', 0, 'intval');
        $url = addons_url('Draw://Wap/setLuckyFollow');
        // 奖品列表
        $data = D('LotteryPrizeList')->getList($sports_id);
        foreach ($data as $v) {
            if ($v ['award_num'] != 0) {
                // 让实物排在前面
                if ($v ['awardarr'] ['award_type'] == 1) {
                    $sd [] = $v;
                } else {
                    $d [] = $v;
                }
            }
        }
        // $prizes = array_chunk ( $d, 3, true );
        // 添加最后一项谢谢参与
        $unPrize ['award_pic'] = SITE_URL . "/Public/static/football/ungeted_pic.png";
        $unPrize ['award_name'] = "谢谢参与";
        $unPrize ['award_id'] = 0;
        // dump($sd);
        // dump($d);
        if (empty ($d)) {
            $prizes = $sd;
        } else {
            $prizes = array_merge($sd, $d);
        }
        // dump($prizes);
        $prizes [] = $unPrize;
        $this->assign('savefollow', $url);
        $this->assign('sports_id', $sports_id);
        $this->assign('prizelist', $prizes);
        $followInfo = get_followinfo($this->mid);
        // dump($followInfo);
        $this->assign('followInfo', $followInfo);

        // dump($prizes);
        $this->display();
    }

    // 获取随机抽取的奖品id
    function getChouZPrizeId()
    {
        $prizeid = 0;
        $follow_id = $this->mid;
        $sports_id = I('sports_id', 0, 'intval');
        $drawFollowLog = D('DrawFollowLog');
        $manager_id = session('manager_id');
        $count = $drawFollowLog->hasDraw($sports_id, $follow_id);
        if ($count) {
            $drawFollowLog->updateCount($sports_id, $follow_id, $count);
        } else {
            $data ['sports_id'] = $sports_id;
            $data ['follow_id'] = $follow_id;
            $data ['count'] = 1;
            $data ['cTime'] = NOW_TIME;
            $data ['uid'] = $manager_id;
            $drawFollowLog->delayAddData($data);
        }
        $luckyFollowPrize = D('LuckyFollow')->getUserPrizeData($sports_id, $follow_id);
        $xuni_num = 0;
        $shiwu_num = 0;
        $numConfig = get_addon_config('Sports');
        foreach ($luckyFollowPrize as $v) {
            if ($v ['award_type'] == 0 || $v ['award_type'] == 1) {
                if ($v ['award_type'] == 0) {
                    $xuni_num++;
                }
                if ($v ['award_type'] == 1) {
                    $shiwu_num++;
                }
                // $hasAward [] = $v ['award_type'];

                if ($xuni_num == intval($numConfig ['xuni_limit']) && $shiwu_num == intval($numConfig ['shiwu_limit'])) {
                    $returnData ['award_type'] = -1;
                    $returnData ['prize_id'] = $prizeid;
                    $this->ajaxReturn($returnData, "JSON");
                    exit ();
                }
            }
        }
        $prizelist = D('LotteryPrizeList')->getList($sports_id);
        foreach ($prizelist as $v) {
            if ($v ['award_num'] != 0) {
                if ($v ['awardarr'] ['award_type'] == 1) {
                    $shiwu [] = $v;
                } else if ($v ['awardarr'] ['award_type'] == 0) {
                    // $xuni[]=$v;
                    $xuniPrize [$v ['award_id']] = $v ['award_num'];
                }
                // $d [] = $v;
            }
        }
        // dump($prizelist);
        // dump($xuniPrize);
        // $prizeid=get_jifen_lottery($xuniPrize);
        // dump($r);
        // die;
        foreach ($shiwu as $v) {
            $shiwuArr [] = array(
                'prize_id' => $v ['award_id'],
                'prize_num' => $v ['award_num']
            );
        }
        // foreach ( $xuni as $v ) {
        // $xuniArr [] = array (
        // 'prize_id' => $v ['award_id'],
        // 'prize_num' => $v ['award_num']
        // );
        // }
        $prizeArr ['shiwu'] = $shiwuArr;
        // $prizeArr['xuni']=$xuniArr;
        // foreach ( $d as $v ) {
        // $prizeArr [] = array (
        // 'prize_id' => $v ['award_id'],
        // 'prize_num' => $v ['award_num']
        // );
        // }
        $sports = D('Addons://Sports/Sports')->getInfo($sports_id);
        $start_time = $sports ['start_time'];
        $end_time = $start_time + 120 * 60;

        // if ($end_time < NOW_TIME) {
        // $prizeid = - 1;
        // } else {
        $data = get_lottery($prizeArr, $start_time, $end_time, $sports_id);
        // dump($data);
        $dao = D('Addons://Draw/Award');
        if (!empty ($data ['shiwu'])) {
            foreach ($data ['shiwu'] as $k => $v) {
                // echo NOW_TIME;
                if ($k <= NOW_TIME) {
                    if ($v != 0) {
                        // $prizeInfo = $dao->getInfo ( $v );
                        if ($shiwu_num < intval($numConfig ['shiwu_limit'])) {
                            $prizeid = $v;
                            del_lottery($k, $sports_id, 'shiwu');
                            break;
                        } else if ($xuni_num < intval($numConfig ['xuni_limit'])) {
                            $prizeid = get_jifen_lottery($xuniPrize);
                            // foreach ($data['xuni'] as $k =>$v){
                            // $prizeid=$v;
                            // del_lottery($k,$sports_id,'xuni');
                            // break;
                            // }
                            break;
                        }
                    }
                } else {
                    if ($xuni_num < intval($numConfig ['xuni_limit'])) {
                        $prizeid = get_jifen_lottery($xuniPrize);
                        // 随机获取积分
                        // foreach ($data['xuni'] as $k =>$v){
                        // $prizeid=$v;
                        // del_lottery($k,$sports_id,'xuni');
                        // break;
                        // }
                    }

                    break;
                }
            }
        } else {
            if ($xuni_num < intval($numConfig ['xuni_limit'])) {
                $prizeid = get_jifen_lottery($xuniPrize);
            }
        }

        // foreach ( $data as $k => $v ) {
        // // echo NOW_TIME;
        // if ($k <= NOW_TIME) {
        // if ($v != 0) {
        // $prizeInfo = $dao->getInfo ( $v );
        // if ($hasAward [0] != $prizeInfo ['award_type']) {
        // $prizeid = $v;
        // del_lottery ( $k, $sports_id );
        // break;
        // } else {
        // break;
        // }
        // }
        // }
        // }
        // }
        // dump( get_lottery ( $prizeArr, $start_time, $end_time, $sports_id ));
        // dump(NOW_TIME);
        // dump($prizeid);die();
        $followInfo = get_followinfo($follow_id);
        if ($prizeid != 0) {
            $award = D('Award')->getInfo($prizeid);
            if ($award ['award_type'] == 0) {
                $data ['sport_id'] = $sports_id;
                $data ['award_id'] = $prizeid;
                $data ['follow_id'] = $follow_id;
                // $data ['truename'] = $name;
                // $data ['mobile'] = $phone;
                $data ['zjtime'] = NOW_TIME;
                $data ['num'] = 1;
                $data ['state'] = 1;
                $data ['djtime'] = NOW_TIME;
                $data ['uid'] = $manager_id;
                $res = D('LuckyFollow')->delayAddData($data);

                $new_score = $followInfo ['score'] + intval($award ['score']);
                D('Common/Follow')->updateField($data ['follow_id'], 'score', $new_score);
                // 奖品数量减1
                // D ( 'LotteryPrizeList' )->setCount ( $data ['sport_id'], $data ['award_id'] );
            } else {
                $data ['sport_id'] = $sports_id;
                $data ['award_id'] = $prizeid;
                $data ['follow_id'] = $follow_id;
                $data ['zjtime'] = NOW_TIME;
                $data ['num'] = 1;
                $data ['state'] = 0;
                $data ['uid'] = $manager_id;
                $res = D('LuckyFollow')->delayAddData($data);
            }
        }
        if ($followInfo ['truename'] && $followInfo ['mobile']) {
            $returnData ['followinfo'] = 1;
        } else {
            $returnData ['followinfo'] = 0;
        }

        if ($award ['award_type'] != null && ($award ['award_type'] == 0 || $award ['award_type'] == 1)) {
            $returnData ['award_type'] = $award ['award_type'];
        } else {
            $returnData ['award_type'] = -1;
        }
        $returnData ['prize_id'] = $prizeid;
        $this->ajaxReturn($returnData, "JSON");
    }

    // 保存中奖信息信息
    function setLuckyFollow()
    {
        $name = I('post.name');
        $phone = I('post.phone');

        if (empty ($name)) {
            $returnData ['msg'] = "请输入姓名";
            $returnData ['result'] = "fail";
            $this->ajaxReturn($returnData, "JSON");
            exit ();
        }
        if (empty ($phone) || strlen($phone) != 11) {
            $returnData ['msg'] = "请输入正确的手机号码";
            $returnData ['result'] = "fail";
            $this->ajaxReturn($returnData, "JSON");
            exit ();
        }

        // $data ['sport_id'] = I ( 'post.sportsId' );
        // $data ['award_id'] = I ( 'post.thePrizeId' );
        $data ['follow_id'] = $this->mid;
        $data ['truename'] = $name;
        $data ['mobile'] = $phone;
        // $data ['zjtime'] = NOW_TIME;
        // $data ['num'] = 1;

        // 当粉丝在同一个场次获得同一个奖时，设置数量
        // $r = D ( 'LuckyFollow' )->setNum ( $data );
        // if (!empty($r)) {
        // $returnData ['msg'] = "添加成功";
        // $returnData ['result'] = "success";
        // $this->ajaxReturn ( $returnData, "JSON" );
        // exit ();
        // }
        // $res = D ( 'LuckyFollow' )->delayAddData ( $data );

        $res = D('Common/Follow')->update($data ['follow_id'], $data);
        if ($res) {
            $returnData ['msg'] = "添加成功";
            $returnData ['result'] = "success";
            $this->ajaxReturn($returnData, "JSON");
        }
    }
}
